package com.cci.kangdao.task;

import com.cci.kangdao.cipapi.CipApiUtils;
import com.cci.kangdao.cipapi.CipResponeEntity;
import com.cci.kangdao.cipapi.EsnBasicInfoEntity;
import com.cci.kangdao.dao.CustomerEngineRelTDao;
import com.cci.kangdao.dao.DictionariesTDao;
import com.cci.kangdao.dao.EngineMsgDao;
import com.cci.kangdao.utilTool.CollectionUtils;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import com.cci.kangdao.utilTool.StringToool;
import com.gexin.fastjson.JSON;
import net.sf.json.JSONObject;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author linjiankun
 * @date 2023/2/14 17:19
 */
@Component
public class UpdateEsnAppPowerTypeTask {


    private Logger log = Logger.getLogger(this.getClass().getName());

    @Autowired
    private EngineMsgDao engineMsgDao;
    @Autowired
    private CustomerEngineRelTDao customerEngineRelTDao;
    @Autowired
    private DictionariesTDao dictionariesTDao;

    //每个月27号零点5分执行的定时任务0 5 0 27 * ?     因服务器时间小于现实时间8小时  所以这里cron表达式为0 5 16 26 * ?
    public void batchUpdateEsnAppPowerType(){
        String currentTime = LocalTimeUtil.getRealCurrentTime(); //获取当前时间
        log.info("定时更新用户已绑定设备的发动机基础数据表enginemsg的动力类型(0：天然气   1：柴油)" +
                "和应用类型(0：道路车辆   1：工程机械)任务开始，当前时间为：" + currentTime);
        try{
            publicTask("");
        }catch(Exception e) {
            log.error("定时更新用户已绑定设备的发动机基础数据任务发生异常，异常发生时间："+currentTime);
            log.error(e.getMessage(), e);
        }finally {
            log.info("定时更新用户已绑定设备的发动机基础数据表enginemsg的动力类型(0：天然气   1：柴油)" +
                    "和应用类型(0：道路车辆   1：工程机械)任务结束，当前时间为：" + currentTime);
        }
    }



    public void publicTask(String param) {
        List<String> stringList = StringToool.getStringList(param, ",");
        List<Map<String, Object>> esnMapList = customerEngineRelTDao.getCustomerBindingESNList(stringList);
        if (CollectionUtils.isNotEmpty(esnMapList)) {
            esnMapList.stream().forEach(esnMap -> {
                String esn = MapUtils.getString(esnMap, "ESN", "");
                if (StringUtils.isNotEmpty(esn)) {
                    int fleetEmissionLevelDInt = -1;
                    int fleetFuelTypeDInt = -1;
                    //调用cip接口获取esn信息
                    EsnBasicInfoEntity esnInfo = null;
                    CipResponeEntity respone = CipApiUtils.getCipServiceApi().getEsnInfo(esn);
                    if (null != respone && respone.getCode().equals(0)) {
                        esnInfo = (EsnBasicInfoEntity) JSONObject.toBean(respone.getData(), EsnBasicInfoEntity.class);
                        //排放等级
                        String emissionLevel = esnInfo.getEmissionLevel();
                        //燃油类型
                        String fuelType = esnInfo.getFuelType();
                        //获取字典表应用类型map(调用cip接口返回的应用类型枚举值)
                        Map<String,Map<String,Object>> fleetEmissionLevelDicListMap = dictionariesTDao.getDicListByDtableName("fleetEmissionLevel");
                        if(MapUtils.isNotEmpty(fleetEmissionLevelDicListMap) && StringUtils.isNotEmpty(emissionLevel) && fleetEmissionLevelDicListMap.containsKey(emissionLevel)){
                            Map<String,Object> fleetEmissionLevelDicMap = MapUtils.getObject(fleetEmissionLevelDicListMap,emissionLevel);
                            if(MapUtils.isNotEmpty(fleetEmissionLevelDicMap)){
                                fleetEmissionLevelDInt = MapUtils.getIntValue(fleetEmissionLevelDicMap,"DInt",-1);
                            }
                        }
                        //获取字典表动力类型map(调用cip接口返回的应用类型枚举值)
                        Map<String,Map<String,Object>> fleetFuelTypeDicListMap = dictionariesTDao.getDicListByDtableName("fleetFuelType");
                        if(MapUtils.isNotEmpty(fleetFuelTypeDicListMap) && StringUtils.isNotEmpty(fuelType) && fleetFuelTypeDicListMap.containsKey(fuelType)){
                            Map<String,Object> fleetFuelTypeDicMap = MapUtils.getObject(fleetFuelTypeDicListMap,fuelType);
                            if(MapUtils.isNotEmpty(fleetFuelTypeDicMap)){
                                fleetFuelTypeDInt = MapUtils.getIntValue(fleetFuelTypeDicMap,"DInt",-1);
                            }
                        }
                        esnMap.put("EsnApplicationType",fleetEmissionLevelDInt);
                        esnMap.put("EsnPowerType",fleetFuelTypeDInt);
                        engineMsgDao.updateEngineMsg(esnMap);
                    }
                }
            });
        }
    }

}
